package com.etc.controller;

import com.etc.entity.FxAdmin;
import com.etc.entity.FxMusician;

import com.etc.entity.User;
import com.etc.service.UserService;
import com.etc.util.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("")
//
@CrossOrigin
@Api(value = "用户",description = "用户接口")
public class UserController {
    @Autowired
    UserService service;

    @ApiOperation(value = "登入", notes = "用户登入接口",response = User.class,code = 200)
    @GetMapping(value = "User_Login")
    public ResponseData Select_UserLogin(@RequestParam(value = "phone", defaultValue = "") String phone, @RequestParam(value = "password", defaultValue = "") String password){
        List<User> list = service.select_UserLogin(phone, password);
        ResponseData responseData = new ResponseData();
        responseData.setData(list);
        return responseData;
    }

    @ApiOperation(value = "", notes = "查询id用户",response = User.class,code = 200)
    @GetMapping(value = "User_ById/{id}")
    public ResponseData Select_FxUserByPrimaryKey(@PathVariable int id){
        ResponseData responseData = new ResponseData();
        List<User> list = service.selectUserByPrimaryKey(id);
        responseData.setData(list);
        return responseData;
    }

    @ApiOperation(value = "注册", notes = "用户注册接口",response = User.class,code = 200)
    @PostMapping(value = "User_Insert")
    public ResponseData Insert_User(@RequestBody User fxUser){
        ResponseData responseData = new ResponseData();
        //判断 注册的用户号码与其他用户号码是否重复
        List<User> list = service.select_All();
        for (User u: list){
            if (fxUser.getUserphone().equals(u.getUserphone())){
                responseData.setMsg("用户号码重复");
                return responseData;
            }
        }
        boolean flag = service.insert_User(fxUser);
        if (!flag){
            responseData.setMsg("filed");
        }
        return responseData;
    }

    @ApiOperation(value = "修改用户信息", notes = "用户修改接口",response = User.class,code = 200)
    @PutMapping(value = "UserInfo_Update/{id}")
    public ResponseData Update_UserByPrimaryKeySelective(@PathVariable int id, @RequestBody User record){
        ResponseData responseData = new ResponseData();
        record.setId(id);
        boolean flag = service.update_UserByPrimaryKeySelective(record);
        if (!flag){
            responseData.setMsg("修改失败");
        }else {
            responseData.setMsg("修改成功");
        }

        return responseData;
    }

    @ApiOperation(value = "修改用户密码", notes = "用户修改接口",response = User.class,code = 200)
    @PutMapping(value = "UserPassword_Update/{id}")
    public ResponseData Update_UserPassword(@PathVariable int id ,@RequestParam(value = "password", defaultValue = "") String password, @RequestParam(value = "pwd", defaultValue = "") String pwd){
        ResponseData responseData = new ResponseData();

        if (password.equals(pwd)){
            boolean flag = service.update_UserPassword(id, password);
            if (!flag) {
                responseData.setMsg("修改失败");
            }
        }else {
            responseData.setMsg("前后密码不一致");
        }
        return responseData;
    }
}
